﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace simpleHotel.Business
{
    public class BookingBLL
    {
        public string GenerateBookingCode(int provinceId,int hotelId)
        {
            string res = string.Empty;
            string region = FindRegion(provinceId);
            string count = CountBooking(hotelId);
            res = region.Trim() + DateTime.Now.ToString("ddMMyy") + count;

            return res;
        }

        private string CountBooking(int hotelid)
        {
            int count = 0;
            string res = string.Empty;

            var model = Singleton.NewDataContext();
            var sql = from b in model.Bookings
                      where b.HotelID == hotelid
                      select b;

            count = sql.Count();
            count = count + 1;

            if (count < 10) res = "000" + count.ToString();
            else if (count < 100) res = "00" + count.ToString();
            else if (count < 1000) res = "0" + count.ToString();
            else res = count.ToString();

            return res;
        }

        private string FindRegion(int provinceId)
        {
            string res = string.Empty;
            var model = Singleton.NewDataContext();
            var sql=from p in model.Provinces.Where(c=>c.ID==provinceId)
                    join r in model.Regions on p.RegionID equals r.ID
                    select new {p, r};

            res = sql.FirstOrDefault().r.RegionCode;

            return res;
        }


        public int InsertCustomer(string fullname, string CMND, string address, string phone, string email)
        {
            var model = Singleton.NewDataContext();
            ////model.Connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SimpleHotel_ConStr"].ToString();

            var c = new Customer();

            c.FullName = fullname;
            c.Passport = CMND;
            c.Address = address;
            c.Phone = phone;
            c.Email = email;

            model.Customers.InsertOnSubmit(c);
            model.SubmitChanges();

            return c.Id;
        }

        public int InsertBookingInfo(int customerid,
                                     int hotelid,
                                     int paymentid,
                                     decimal totalpaid,
                                     DateTime checkindate,
                                     DateTime checkoutdate,
                                     int status,
                                     int numberofroom,
                                     int categoryroomid,
                                     string bookingCode)
                                     
        {
            var model = Singleton.NewDataContext();
            ////model.Connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SimpleHotel_ConStr"].ToString();

            var b = new Booking();
            b.CustomerID = customerid;
            b.HotelID = hotelid;
            b.PaymentID = paymentid;
            b.TotalPaid = totalpaid;
            b.CheckInDate = checkindate;// Convert.ToDateTime(checkindate);
            b.CheckOutDate = checkoutdate;// Convert.ToDateTime(checkoutdate);
            b.Status = status;
            b.NumberOfRooms = numberofroom;
            b.CategoryRoomID = categoryroomid;
            b.BookingCode = bookingCode;
            b.CreatedDate = DateTime.Now;
            model.Bookings.InsertOnSubmit(b);
            model.SubmitChanges();

            return b.ID;
        }

        public void UpdateBookingInfo(string transfer, int paystatus)
        {
             var model = Singleton.NewDataContext();
             ////model.Connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SimpleHotel_ConStr"].ToString();

            //string s=transfer.Substring(0,transfer.Length-1);
            string[] arr = transfer.Split('*');

            if (arr.Length > 0)
            {
                for(int i=0;i<arr.Length;i++)
                {
                    if (arr[i] != "")
                    {
                        var query = from b in model.Bookings
                                    where b.ID == Int32.Parse(arr[i])
                                    select b;

                        foreach (Booking b in query)
                        {
                            b.Status = paystatus;
                        }

                        model.SubmitChanges();
                    }
                }
            }
        }
    }
}
